package com.j1.oauth.userdetails;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserDao userDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserInfo sysUserInfo = userDao.findByUsername(username);
        if (sysUserInfo !=null){
            SimpleGrantedAuthority authority1 = new SimpleGrantedAuthority("list");
            SimpleGrantedAuthority authority2 = new SimpleGrantedAuthority("query");
            List<GrantedAuthority> authority = Arrays.asList(authority1,authority2 );
            User user = new User(sysUserInfo.getUsername(), passwordEncoder.encode(sysUserInfo.getPassword()), authority);
            return user;
        }
        return null;
    }
}
